package com.jl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.DamageComponent;
import com.jl.service.IDamageComponentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 损坏(更换)零件表 前端控制器
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/damageComponent")
public class DamageComponentController extends BaseController {

    @Autowired
    private IDamageComponentService damageComponentService;

    @GetMapping("/damageComponentDataGrid")
    @RequiresPermissions("/damageComponent/damageComponentDataGrid")
    public String damageComponentList(){
        return "damageComponent_list";
    }


    @GetMapping("/add")
    @RequiresPermissions("/damageComponent/add")
    public String addDamageComponentPage(ModelMap mp, Long id){
        mp.put("id",id);
        return "damageComponent_add";
    }

    /**
     * 一定是add
     * @param json
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("/damageComponent/add")
    @ResponseBody
    public ResultData addDamageComponent(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DamageComponent damageComponent = JSON.parseObject(value,DamageComponent.class);
        damageComponent.setCreateId(getUserId());
        damageComponent.setGmtCreate(new Date());
        
        damageComponentService.save(damageComponent);
        return success(damageComponent);
    }



    @GetMapping("/edit")
    @RequiresPermissions("/damageComponent/edit")
    public String editDamageComponentPage(ModelMap mp, Long id){
        mp.put("id",id);
        return "damageComponent_add";
    }

    /**
     * 一定是edit
     * @param json
     * @return
     */
    @PostMapping("/edit")
    @RequiresPermissions("/damageComponent/edit")
    @ResponseBody
    public ResultData editDamageComponent(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        DamageComponent damageComponent = JSON.parseObject(value,DamageComponent.class);
        //表示是修改
        damageComponent.setModifyId(getUserId());
        damageComponent.setGmtModified(new Date());

        damageComponentService.updateById(damageComponent);
        return success(damageComponent);
    }


    @PostMapping("/delete")
    @RequiresPermissions("/damageComponent/delete")
    @ResponseBody
    public ResultData deletesDamageComponent(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        boolean b = damageComponentService.removeByIds(ids);
        if(b){
            return success();
        }
        return fail();
    }

    /**
     * 一定是dataGrid
     * @param current
     * @param pageSize
     * @param searchCol
     * @param searchValue
     * @return
     */
    @PostMapping("/dataGrid")
    @RequiresPermissions("/damageComponent/dataGrid")
    @ResponseBody
    public ResultData damageComponentDataGrid(Long current,Long pageSize,String searchCol,String searchValue,String json){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<DamageComponent> queryWrapper = new QueryWrapper<>();
       if(StringUtils.isNotBlank(json)){
           String value = BASE64.decode(json);
           JSONObject jsonObject = JSONObject.parseObject(value);
           if(StringUtils.isNotBlank(jsonObject.getString("name"))){
               queryWrapper.like("t.name",jsonObject.getString("name"));
           }
           if(StringUtils.isNotBlank(jsonObject.getString("deviceNumber"))){
               queryWrapper.like("t.device_number",jsonObject.getString("deviceNumber"));
           }
           if(null!=jsonObject.getLong("createId") && jsonObject.getLong("createId")!=0){
               queryWrapper.eq("t.create_id",jsonObject.getString("createId"));
           }

       }else{
           if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
               if(searchCol.equals(Constants.ID)){
                   queryWrapper.eq("t.id",searchValue);
               }else{
                   queryWrapper.like("t."+searchCol,searchValue);
               }
           }
       }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);

        damageComponentService.selectDamageComponentDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }


    @GetMapping("/easyDamage")
    @RequiresPermissions("/damageComponent/easyDamage")
    public String easyDamageComponentPage(){
        return "easyDamageComponent_list";
    }

    @PostMapping("easyDamage")
    @RequiresPermissions("/damageComponent/easyDamage")
    @ResponseBody
    public ResultData easyDamageComponent(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<DamageComponent> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
            if(searchCol.equals(Constants.ID)){
                queryWrapper.eq("t.id",searchValue);
            }else{
                queryWrapper.like("t."+searchCol,searchValue);
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        damageComponentService.selectEasyDamageComponent(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }

    @PostMapping("deviceDamageComponent")
    @ResponseBody
    public ResultData deviceDamageComponent(Long current,Long pageSize,Long deviceId){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<DamageComponent> queryWrapper = new QueryWrapper<>();
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        damageComponentService.selectDamageComponentByDeviceId(pageDataInfo,deviceId);
        return success(pageDataInfo);
    }

}
